{extend name="platform/base" /}
{block name="main"}
<section class="panel panel-default">
	<header class="panel-heading font-bold"> 添加模块 </header>
	<div class="panel-body">
		<form class="form-horizontal" id="form" name="form" onsubmit="addModuleAjax()">
			<div class="form-group">
				<label class="col-sm-2 control-label"><span class="color-red">*</span>模块名</label>
				<div class="col-md-4">
					<input type="text" class="form-control" id="module_name" value="" onkeydown="error_hide()">
				</div>
				<p class="error">请输入模块名</p>
			</div>
			<div class="line line-dashed line-lg pull-in"></div>
			<div class="form-group">
				<label class="col-sm-2 control-label">上級菜单</label>
				<div class="col-sm-4">
					<select id="pid" name="account" class="form-control m-b">
						<option value="0">顶级菜单</option> 
						{foreach name="list" item="v1"}
						<option value="{$v1['data']['module_id']}" >&nbsp;&nbsp;&nbsp;{$v1['data']['module_name']}</option>
						{foreach name="v1['sub_menu']" item="v2"}
						<option value="{$v2['module_id']}" >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{$v2['module_name']}</option>
						{/foreach} {/foreach}
					</select>
				</div>
			</div>
			<div class="line line-dashed line-lg pull-in"></div>
			<div class="form-group">
				<label class="col-sm-2 control-label"><span class="color-red">*</span>控制器名</label>
				<div class="col-md-4">
					<input type="text" class="form-control" id="controller" value="" onkeydown="error_hide()">
				</div>
				<p class="error">请输入控制器名</p>
			</div>
			<div class="line line-dashed line-lg pull-in"></div>
			<div class="form-group">
				<label class="col-sm-2 control-label"><span class="color-red">*</span>方法名</label>
				<div class="col-md-4">
					<input type="text" class="form-control" id="method" value="" onkeydown="error_hide()">
				</div>
				<p class="error">请输入方法名</p>
			</div>
			<div class="line line-dashed line-lg pull-in"></div>
			<div class="form-group">
				<label class="col-sm-2 control-label"><span class="color-red">*</span>url</label>
				<div class="col-md-4">
					<input type="text" class="form-control" id="url" value="" onkeydown="error_hide()">
				</div>
				<p class="error">请输入url地址</p>
			</div>
			<div class="line line-dashed line-lg pull-in"></div>
			<div class="form-group">
                                            <label class="col-sm-2 control-label">是否是菜单</label>
                                            <div class="col-sm-4">
                                                <label class="switch">
                                                    <input id="is_menu" type="checkbox" >
                                                    <span></span> </label>
                                            </div>
                                        </div>
                                        
			<div class="line line-dashed line-lg pull-in"></div>
			<div class="form-group">
                                            <label class="col-sm-2 control-label">是否控制权限</label>
                                            <div class="col-sm-4">
                                                <label class="switch">
                                                    <input id="is_control_auth" type="checkbox" checked="checked" >
                                                    <span></span> </label>
                                            </div>
                                        </div>
            <div class="line line-dashed line-lg pull-in"></div>
			<div class="form-group">
                                            <label class="col-sm-2 control-label">是否仅开发者模式可见</label>
                                            <div class="col-sm-4">
                                                <label class="switch">
                                                    <input id="is_dev" type="checkbox" >
                                                    <span></span> </label>
                                            </div>
                                        </div>
			<div class="line line-dashed line-lg pull-in"></div>
			<div class="form-group">
				<label class="col-sm-2 control-label">排序</label>
				<div class="col-md-2">
					<input type="text" class="form-control" id="sort" value="">
				</div>
			</div>
			<div class="line line-dashed line-lg pull-in"></div>
 			<!-- 上传图片 -->
			<div class="form-group">
                <label class="col-sm-2 control-label">图片</label>
                <div class="col-sm-2">
                	<p class="imgLogo-box"><img id="imgLogo" src="" class="imglogo"/></p>
                	<a href="javascript:void(0);" style="display:block;position:relative;z-index:1;">
                		<span style="width: 97px;height: 34px;position: absolute;left: 0;top: 0;z-index: 2;cursor: pointer;"> 
                		<input hidefocus="true" size="1" class="input-file" name="file_upload"
							id="uploadImg" nc_type="change_store_label" type="file" style="width: 97px;height: 34px;padding: 0;margin: 0;border: none 0;opacity: 0;filter: alpha(opacity = 0);cursor: pointer;"
							onchange="imgUpload(this);"> 
							<input type="hidden" id="Logo" value="" />
						</span>
						<a class="btn btn-default"><i class="fa fa-cloud-upload text"></i><span class="text">上传图片</span></a>
					</a>
                </div>
            </div>
			
			<div class="line line-dashed line-lg pull-in"></div>
			<div class="form-group">
				<label class="col-sm-2 control-label">矢量图Class</label>
				<div class="col-md-4">
					<input type="text" class="form-control" id="icon_class" value="">
				</div>
			</div>
			<div class="line line-dashed line-lg pull-in"></div>
			<div class="form-group">
                                            <label class="col-sm-2 control-label">描述</label>
                                            <div class="col-sm-4">
                                                <div id="editor" class="form-control" style="overflow:scroll;height:150px;max-height:150px"></div>
                                            </div>
                                        </div>
			<!-- 书签 -->
			<div class="line line-dashed line-lg pull-in"></div>
			<div class="form-group">
				<div class="col-sm-4 col-sm-offset-2">
					<a href="javascript:void(0);" onclick="addModuleAjax()" class="btn btn-primary">保存</a>
				</div>
			</div>
		</form>
	</div>
</section>
</div>
<script>	
	/**
	*添加模块
	*/
	function addModuleAjax() {
		var pid = $("#pid").val();
		var module_name = $("#module_name").val();
		var controller = $("#controller").val();
		var method = $("#method").val();
		var url = $("#url").val();
		var is_menu = $("#is_menu").val();
		var is_control_auth = 1;//是否控制权限
		var is_dev = $("#is_dev").val();
		var sort = $("#sort").val();
		var desc = $("#editor").html();
		if(module_name == ''){
			$("#module_name").focus().parent().next().show();
			return false;
		}
		if(controller == ''){
			$("#controller").focus().parent().next().show();
			return false;
		}
		if(method == ''){
			$("#method").focus().parent().next().show();
			return false;
		}
		if(url == ''){
			$("#url").focus().parent().next().show();
			return false;
		}
		if($("#is_menu").prop("checked")){
			var is_menu = 1;
		}else{
			var is_menu = 0;
		}
		if(!$("#is_control_auth").prop("checked")){
			is_control_auth = 0;
		}
		if($("#is_dev").prop("checked")){
			var is_dev = 1;
		}else{
			var is_dev = 0;
		}
		var module_picture = $("#Logo").val();
		var icon_class = $('#icon_class').val();
		//verify(module_name, controller, method, url, sort, desc);
		$.ajax({
			type : "post",
			url : "{:__URL('PLATFORM_MAIN/System/addModule')}",
			data : {
				'pid' : pid,
				'module_name' : module_name,
				'controller' : controller,
				'method' : method,
				'url' : url,
				'is_menu' : is_menu,
				'is_control_auth' : is_control_auth,
				'is_dev' : is_dev,
				'sort' : sort,
				'module_picture' : module_picture,
				'desc' : desc,
				'icon_class' : icon_class
			},
			async : true,
			success : function(data) {
				if (data["code"] > 0) {
					showMessage('success', "添加成功", "{:__URL('PLATFORM_MAIN/System/moduleList')}");
				}else{
					showMessage('error', "添加失败");
				}	
			}
		});
	}
	/**
	*模块输入信息验证
	*/
	function verify( module_name, controller, method, url,  sort, desc){
		if(module_name == ''){
			$("#module_name").parent().next().show();
			return flase;
		}else{
			$(".error").hide();
		}
		if(controller == ''){
			$("#controller").parent().next().show();
			return false;
		}else{
			$(".error").hide();
		}
		if(method == ''){
			$("#method").parent().next().show();
			return false;
		}else{
			$(".error").hide();
		}
		if(url == ''){
			$("#url").parent().next().show();
			return false;
		}else{
			$(".error").hide();
		}
		if(sort == ''){
			$("#sort").parent().next().show();
			return false;
		}else{
			$(".error").hide();
		}
		if(desc == ''){
			$("#desc").parent().next().show();
			return false;
		}else{
			$(".error").hide();
		}
	}
	/**
	 *图片上传
	 */
	/*function imgUpload(event) {
		var fileid = $(event).attr("id");   var str = $(event).next().attr("id");
		fileCommonUpload("UPLOAD_URL", fileid,"__UPLOAD__",str);
	}*/
	//图片上传
	function imgUpload(event) {
		var fileid = $(event).attr("id");
		var data = { 'file_path' : UPLOADADV };
		uploadFile(fileid,data,function(res){
			if(res.code){
				$("#imgLogo").attr("src",__IMG(res.data));
				$("#Logo").val(res.data);
				showTip(res.message,"success");
			}else{
				showTip(res.message,"error");
			}
		});
	}
</script>
{/block}